home *** CD-ROM | disk | FTP | other *** search
/ All for Cell Phones: Sony Ericsson / Sony-Ericsson 2004.iso / Java / HTetris / htetris.jar / tetris / HighScores.class (.txt) < prev    next >
Encoding:
Java Class File  |  2002-07-19  |  6.3 KB  |  185 lines

  1. package tetris;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.DataInputStream;
  6. import java.io.DataOutputStream;
  7. import javax.microedition.lcdui.Command;
  8. import javax.microedition.lcdui.CommandListener;
  9. import javax.microedition.lcdui.Display;
  10. import javax.microedition.lcdui.Displayable;
  11. import javax.microedition.lcdui.Form;
  12. import javax.microedition.lcdui.TextField;
  13. import javax.microedition.rms.RecordStore;
  14.  
  15. public class HighScores extends Form implements CommandListener {
  16.    static String[] names = new String[]{"", "", "", "", "", "", "", "", "", ""};
  17.    static int[] scores = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  18.    static TextField readName;
  19.    static RecordStore scoresRS;
  20.  
  21.    public HighScores() {
  22.       super("High Scores");
  23.  
  24.       try {
  25.          this.jbInit();
  26.       } catch (Exception e) {
  27.          ((Throwable)e).printStackTrace();
  28.       }
  29.  
  30.    }
  31.  
  32.    protected void openStore() {
  33.       try {
  34.          scoresRS = RecordStore.openRecordStore("scores", true);
  35.       } catch (Exception e) {
  36.          ((Throwable)e).printStackTrace();
  37.       }
  38.  
  39.    }
  40.  
  41.    protected void closeStore() {
  42.       try {
  43.          scoresRS.closeRecordStore();
  44.       } catch (Exception e) {
  45.          ((Throwable)e).printStackTrace();
  46.       }
  47.  
  48.    }
  49.  
  50.    public void updateScores() {
  51.       while(((Form)this).size() > 0) {
  52.          ((Form)this).delete(0);
  53.       }
  54.  
  55.       for(int i = 0; i < 10 && scores[i] > 0; ++i) {
  56.          ((Form)this).append(String.valueOf(String.valueOf((new StringBuffer(String.valueOf(String.valueOf(i == 0 ? "" : "\n")))).append(i < 9 ? " " : "").append(i + 1).append(". ").append(names[i]).append(" [").append(scores[i]).append("]"))));
  57.       }
  58.  
  59.    }
  60.  
  61.    private void jbInit() throws Exception {
  62.       ((Displayable)this).setCommandListener(this);
  63.       ((Displayable)this).addCommand(new Command("Back", 7, 1));
  64.       this.openStore();
  65.       readHighScores();
  66.       this.closeStore();
  67.       this.updateScores();
  68.    }
  69.  
  70.    public void commandAction(Command command, Displayable displayable) {
  71.       if (command.getCommandType() == 7) {
  72.          Display.getDisplay(TetrisMIDlet.instance).setCurrent(TetrisMIDlet.mainMenu);
  73.       }
  74.  
  75.       if (command.getCommandType() == 1) {
  76.          this.addScore(TetrisMIDlet.gameScreen.gameScore, readName.getString());
  77.          Display.getDisplay(TetrisMIDlet.instance).setCurrent(TetrisMIDlet.mainMenu);
  78.       }
  79.  
  80.    }
  81.  
  82.    public boolean checkScore(int score) {
  83.       return score > scores[9];
  84.    }
  85.  
  86.    public void getName() {
  87.       Form f = new Form("");
  88.       f.append(readName = new TextField("Enter your name", "", 16, 0));
  89.       ((Displayable)f).setCommandListener(this);
  90.       ((Displayable)f).addCommand(new Command("OK", 1, 1));
  91.       Display.getDisplay(TetrisMIDlet.instance).setCurrent(f);
  92.    }
  93.  
  94.    public void addScore(int score, String name) {
  95.       int i;
  96.       for(i = 0; i < 10 && scores[i] >= score; ++i) {
  97.       }
  98.  
  99.       if (i < 10) {
  100.          for(int j = 9; j > i; --j) {
  101.             scores[j] = scores[j - 1];
  102.             names[j] = names[j - 1];
  103.          }
  104.  
  105.          scores[i] = score;
  106.          names[i] = name;
  107.       }
  108.  
  109.       this.openStore();
  110.       saveHighScores();
  111.       this.closeStore();
  112.    }
  113.  
  114.    public static boolean readHighScores() {
  115.       try {
  116.          if (scoresRS.getNumRecords() > 0) {
  117.             byte[] b = scoresRS.getRecord(1);
  118.             ByteArrayInputStream bais = new ByteArrayInputStream(b);
  119.             DataInputStream dis = new DataInputStream(bais);
  120.  
  121.             for(int i = 0; i < 10; ++i) {
  122.                scores[i] = dis.readInt();
  123.                if (scores[i] == 0) {
  124.                   break;
  125.                }
  126.  
  127.                int j = dis.readInt();
  128.                char[] s = new char[j];
  129.  
  130.                for(int k = 0; k < j; ++k) {
  131.                   s[k] = dis.readChar();
  132.                }
  133.  
  134.                names[i] = new String(s);
  135.             }
  136.  
  137.             try {
  138.                dis.close();
  139.                bais.close();
  140.             } catch (Exception e) {
  141.                ((Throwable)e).printStackTrace();
  142.             }
  143.  
  144.             boolean var9 = true;
  145.             return var9;
  146.          }
  147.       } catch (Exception e) {
  148.          ((Throwable)e).printStackTrace();
  149.       }
  150.  
  151.       return false;
  152.    }
  153.  
  154.    public static void saveHighScores() {
  155.       ByteArrayOutputStream baos = new ByteArrayOutputStream();
  156.       DataOutputStream dos = new DataOutputStream(baos);
  157.  
  158.       try {
  159.          for(int i = 0; i < 10; ++i) {
  160.             dos.writeInt(scores[i]);
  161.             dos.writeInt(names[i].length());
  162.             dos.writeChars(names[i]);
  163.          }
  164.  
  165.          byte[] b = baos.toByteArray();
  166.          if (scoresRS.getNumRecords() > 0) {
  167.             scoresRS.setRecord(1, b, 0, b.length);
  168.          } else {
  169.             scoresRS.addRecord(b, 0, b.length);
  170.          }
  171.       } catch (Exception e) {
  172.          ((Throwable)e).printStackTrace();
  173.       } finally {
  174.          try {
  175.             dos.close();
  176.             baos.close();
  177.          } catch (Exception e) {
  178.             ((Throwable)e).printStackTrace();
  179.          }
  180.  
  181.       }
  182.  
  183.    }
  184. }
  185.